package com.projeto.business.controller;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import com.projeto.persistence.Event;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class EventController {

    @Resource
    protected SessionContext ctx;
    @PersistenceContext(unitName = "final")
    protected EntityManager em;

    public EntityManager getEntityManager() {
        return em;
    }

    public EventController() {
        super();
    }

    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#find(java.lang.String)
     */
    public Event find(String id) {
        return getEntityManager().find(Event.class, id);
    }

    /* (non-Javadoc)
     * @see com.novo.projeto.business.IUserBean#findAll()
     */
    @SuppressWarnings("unchecked")
    public List<Event> findAll() {
        Query query = em.createQuery("SELECT e FROM Event e");
        return (List<Event>) query.getResultList();
    }

    public void save(Event event) {
        try {
            System.out.println(event.toString());
            em.persist(event);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
